package com.foo.common.base.utils.mac;

import java.io.File;
import java.io.IOException;
import java.util.Set;

import org.apache.commons.io.FileUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.foo.common.base.pojo.MavenDependencyModel;
import com.foo.common.base.utils.FooUtils;
import com.foo.common.base.utils.laboratory.xml.PomHelper;
import com.google.common.collect.Sets;
import com.google.common.io.ByteStreams;

/**
 * maven helper of mac version.
 * 
 * @author Steve
 *
 */
public enum MvnMacHelper {
	instance;

	public static final Logger logger = LoggerFactory
			.getLogger(MvnMacHelper.class);

	static boolean offlineMode = false;

	String gitRootPath = "";

	static String projectName = FooUtils.getGlobalArtifactId();

	private static String globalProjectDirectory = FooUtils.getGitHome()
			+ FooUtils.getFileSeparator() + projectName;

	private static File globalPomFile = FileUtils.getFile(globalProjectDirectory
			+ "" + FooUtils.getFileSeparator() + "pom.xml");

	private final String mvnBash = FooUtils.getMvnBash();

	public static void main(String[] args) throws Exception {

		logger.info("analysis pom file start at location of:{}",
				globalPomFile.getAbsolutePath());

		instance.preCheckMavenEnvironment();

		// instance.preCheckMavenEnvironment();
		// instance.copyProjectDenpency();

		// instance.checkProjectDenpencyRealTime();

		// installMavenDenpencyJar(
		// "d:\\tools\\maven\\YeePay_HTMLcommon_V3.0.jar", "com.yeepay",
		// "yeepay", "3.0");
		// installMavenDenpencyJar("d:\\tools\\maven\\ojdbc14-10.2.0.1.0.jar",
		// "com.oracle", "ojdbc14", "10.2.0.1.0");

	}

	public void preCheckMavenEnvironment() throws Exception {

		logger.info("preCheckMavenEnvironment");
		String myCommand = mvnBash + " -version && java -version ";
		Process p = FooUtils.executeCommand(myCommand);
		// + "&& mvn help:active-profiles && mvn dependency:copy-dependencies ";
		ByteStreams.copy(p.getInputStream(), System.out);
		// ByteStreams.copy(p.getErrorStream(), System.err);
		p.waitFor();

	}

	/**
	 * check the newest from site:search.maven.org
	 * 
	 * See site:http://search.maven.org/#api
	 * 
	 * @throws Exception
	 */
	public void checkProjectDenpencyRealTime() throws Exception {

		logger.info("searching from site:search.maven.org");

		Set<MavenDependencyModel> mySet = PomHelper
				.getPomDependencySet(globalPomFile);

		Set<MavenDependencyModel> updateSet = Sets.newTreeSet();
		String newestVersion = "";
		for (MavenDependencyModel mavenDependencyModel : mySet) {
			newestVersion = PomHelper.getPomDependencyNewestVersion(
					mavenDependencyModel.getGroupId(),
					mavenDependencyModel.getArtifactId());
			if (newestVersion.equals(mavenDependencyModel.getVersion())) {
				logger.trace("dependency of:{} do not need update.",
						mavenDependencyModel);
			} else {
				logger.info(
						"dependency of:{} has a newer version from:{} --> {}",
						mavenDependencyModel, mavenDependencyModel.getVersion(),
						newestVersion);
				updateSet.add(mavenDependencyModel);
			}
		}

	}

	/**
	 * Generate all maven dependency jars under folder: /target/dependency. With a clean folder first.
	 * 
	 * @throws IOException
	 * @throws InterruptedException
	 */
	public void copyProjectDenpency() throws IOException, InterruptedException {

		File file = FileUtils.getFile(globalProjectDirectory + "/pom.xml");

		if (file == null) {
			logger.info("pom file do not exist");
			return;
		}

		logger.info("clean dependency directory.");

		File dependencyDir = new File(
				globalProjectDirectory + "/target/dependency");

		if (dependencyDir.isDirectory()) {
			FileUtils.cleanDirectory(dependencyDir);
		}

		logger.info("copying start");

		String myCommand = " cd " + globalProjectDirectory + " && " + mvnBash
				+ " dependency:copy-dependencies -DexcludeScope=provided "
				+ (offlineMode ? "-offline" : "") + "";

		logger.info("command execute is:{}", myCommand);

		Process p = FooUtils.executeCommand(myCommand);
		ByteStreams.copy(p.getInputStream(), System.out);
		ByteStreams.copy(p.getErrorStream(), System.err);
		p.waitFor();

		logger.info("copying end,exit the system.");

	}

	public void installMavenDenpencyJar(String jarFileFullPath, String groupId,
			String artifactId, String version) throws Exception {

		logger.info("install maven denpency");
		String myCommand = "cmd /c D: && mvn install:install-file -Dfile="
				+ jarFileFullPath + " -DgroupId=" + groupId + " -DartifactId="
				+ artifactId + " -Dversion=" + version + " -Dpackaging=jar";
		Process p = FooUtils.executeCommand(myCommand);
		ByteStreams.copy(p.getInputStream(), System.out);
		ByteStreams.copy(p.getErrorStream(), System.err);
		p.waitFor();

	}

}
